import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mirac
 * Date: 2025-08-15
 * Time: 22:32
 */
public class MyStack {
    private int[] arr;
    private int size = 0;

    public MyStack() {
        this.arr = new int[10];
        //栈的初始容量为10
    }

    //入栈
    public int push(int data) {
        if (isFull()) {
            //进行扩容
            this.arr = Arrays.copyOf(this.arr,2*this.arr.length);
        }
        this.arr[this.size] = data;
        this.size++;
        return data;
    }
    //判满
    private boolean isFull() {
        return this.size == this.arr.length;
    }

    //出栈
    public int pop() {
        if (empty()) {
            throw new EmptyStackException("栈为空！");
        }
        int val = this.arr[this.size - 1];
        this.size--;
        return val;
    }
    //检测栈是否为空
    public boolean empty() {
        return this.size == 0;
    }

    //获取栈顶元素
    public int peek() {
        if (empty()) {
            throw new EmptyStackException("栈为空！");
        }
        return this.arr[this.size - 1];
    }

    //获取栈中有效元素个数
    public int size() {
        return this.size;
    }


}
